package com.example.word.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.word.dto.CompRecordDTO;
import com.example.word.dto.TemplateDetailDTO;
import com.example.word.dto.TemplatePartDTO;
import com.example.word.dto.TemplateRecordDTO;
import com.example.word.mapper.TemplateMapper;
import com.example.word.pojo.CompRecord;
import com.example.word.pojo.Template;
import com.example.word.service.CompRecordService;
import com.example.word.mapper.CompRecordMapper;
import com.example.word.util.PageUtils;
import com.example.word.util.UserUtils;
import com.example.word.vo.ConditionVO;
import com.example.word.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class CompRecordServiceImpl extends ServiceImpl<CompRecordMapper, CompRecord>
    implements CompRecordService{

    @Autowired
    private CompRecordMapper compRecordMapper;
    @Autowired
    private TemplateMapper templateMapper;

    @Override
    public PageResult<CompRecordDTO> getUserRecordsByUId(ConditionVO condition) {
        Integer id = UserUtils.getLoginUser().getUserInfoId();
        Integer count = compRecordMapper.selectCount(new LambdaQueryWrapper<CompRecord>()
                .eq(CompRecord::getUId, id));
        if (count == 0) {
            return new PageResult<>();
        }
        List<CompRecordDTO> compRecordDTOS = compRecordMapper.listRecordsByUId(id,PageUtils.getLimitCurrent(), PageUtils.getSize(),condition);
        return new PageResult<>(compRecordDTOS, count);
    }

//    @Override
//    public void deleteUserRecordsById(Integer id) {
//        compRecordMapper.deleteById(id);
//    }

    @Override
    public PageResult<TemplateRecordDTO> getRecordsCountById(ConditionVO condition) {
        Integer id = UserUtils.getLoginUser().getUserInfoId();
        //根据老师id去查模板对比记录，同一个用户的算一个
        Integer count = compRecordMapper.listCountByTId(id);
        if (count == 0) {
            return new PageResult<>();
        }
        List<TemplateRecordDTO> templateRecordDTOS = compRecordMapper.listRecordsCountByTId(id,PageUtils.getLimitCurrent(), PageUtils.getSize(),condition);
        return new PageResult<>(templateRecordDTOS, count);
    }

    @Override
    public PageResult<TemplateDetailDTO> getDetailById(Integer id,ConditionVO condition) {
        List<TemplateDetailDTO> templateDetailDTOS = compRecordMapper.listRecordsByTmpId(id,PageUtils.getLimitCurrent(), PageUtils.getSize(),condition);
        return new PageResult<>(templateDetailDTOS, templateDetailDTOS.size());
    }
}




